package com.zgm.utils;


import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 张公铭 on 2017/5/22 0022.
 */

public class StringUtils
{
    public static String stringToMD5(String value)
    {
        byte[] hash;
        try
        {
            hash = MessageDigest.getInstance("MD5").digest(value.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e)
        {
            Log.e("stringToMD5", "string to MD5 error :" + e.getMessage());
            return null;
        } catch (UnsupportedEncodingException e)
        {
            Log.e("stringToMD5", "string to MD5 error :" + e.getMessage());
            return null;
        }

        StringBuffer hex = new StringBuffer(hash.length * 2);
        for (byte b : hash)
        {
            if ((b & 0xFF) < 0x10)
            {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xff));
        }
        return hex.toString();
    }

    /**
     * 保留2位小数
     *
     * @param data
     * @return
     */
    public static String doubleFormat(double data)
    {
        String value = "";
        DecimalFormat df = new DecimalFormat("#0.00");
        return df.format(data);
    }

    /**
     * 判断是否全是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str)
    {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches())
        {
            return false;
        }
        return true;
    }
}
